<?php
session_start();

require 'Shop.php';


// Diese PHP Datei stellt alle Funktion für die Kommunikation vom Client zum Server bereit.

if (isset($_POST["productId"]) && isset($_POST["quantity"]))
{
	
	$shop = null;
	if (isset($_SESSION["shop"]))
	{
		$shop = unserialize($_SESSION["shop"]);
	}
	else 
	{
		$shop = new Shop("swa015", "swa015", "Pei7Ahr");
	}
 	
	if ($shop != null)
	{
		$shoppingCartMYSQL = new ShoppingCartMySQL("swa015", "swa015", "Pei7Ahr");
		
        $product = $shoppingCartMYSQL->getProduct($_POST["productId"]);
        $shop->add($product->name, $product->price, $_POST["quantity"]);
        $_SESSION["shop"] = serialize($shop);
        
        echo "1";
        return;
	}
	
	echo "0";
	return;
}

if (isset($_POST["checkAuth"]))
{
	
	$shop = null;
	if (isset($_SESSION["shop"]))
	{
		$shop = unserialize($_SESSION["shop"]);
	}
	else
	{
		echo "0";
		return;
	}
	
	if ($shop != null)
	{	
		if ($shop->getIsAuth() == false)
			echo "0";
		else 
			echo "1";
		
		return;
	}
	
	echo "0";
	return;
}

if (isset($_POST["logout"]))
{
	$shop = null;
	if (isset($_SESSION["shop"]))
	{
		$shop = unserialize($_SESSION["shop"]);
	}
	else
	{
		echo "0";
		return;
	}

	if ($shop != null)
	{
		$shop->logout();
		$_SESSION["shop"] = serialize($shop);
		echo "1";
		return;
	}

	echo "0";
	return;
}

if (isset($_POST["password"]) && isset($_POST["username"]))
{
	$shop = null;
	if (isset($_SESSION["shop"]))
	{
		$shop = unserialize($_SESSION["shop"]);
	}
	else
	{
		$shop = new Shop("swa015", "swa015", "Pei7Ahr");
	}
	
	if ($shop != null)
	{
		$shoppingCartMYSQL = new ShoppingCartMySQL("swa015", "swa015", "Pei7Ahr");
		$userid = $shoppingCartMYSQL->getUserID($_POST["username"]);
		
		if (isset($userid))
		{
			$shop->authAndOrder($userid, $_POST["password"]);
			$_SESSION["shop"] = serialize($shop);
			
			echo "1";
			return;
		}
	}
	
	echo "0";
	return;

}